/**
 * Copyright (c) 2024-2025 National Fundamental Software of China Co., Ltd.
 *
 * You should have find a copy of NFSChina License. If not, please contact us
 * by email <os_support@nfschina>
 *
**/

import QtQuick 2.15
import QtQml 2.15
import QtQuick.Layouts 1.15
import QtQuick.Templates 2.15 as T
import QtGraphicalEffects 1.15
import QtQuick.Controls 2.5
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.components 3.0 as PC3
import org.kde.plasma.extras 2.0 as PlasmaExtras
import org.kde.kirigami 2.13 as Kirigami
import org.kde.kcoreaddons 1.0 as KCoreAddons
import org.kde.kquickcontrolsaddons 2.0 as KQuickAddons

Item {
    id: root
    //opacity: 0

    property int currentShowAppsPage: 0  // 0 all apps, 1 apps categories
    property int squareButtonWidth: 36  * PlasmaCore.Units.devicePixelRatio
    property int buttonImageWidth: 16 * PlasmaCore.Units.devicePixelRatio
    KCoreAddons.KUser {
        id: kuser
    }

    PlasmaCore.DataSource {
        id: executable
        engine: "executable"
        connectedSources: []
        onNewData: disconnectSource(sourceName)

        function exec(cmd) {
            executable.connectSource(cmd)
        }
    }

    function action_openMyCompute() {
        executable.exec('nemo desktop:/')
    }

    function action_powerButton() {
        executable.exec('qdbus com.nfs.SessionManager /com/nfs/SessionManager com.nfs.SessionManager.RequestLogout 1')
    }

    ColumnLayout {
        anchors.fill: parent
        spacing: 10
        anchors.bottomMargin: 14
        anchors.topMargin: 12
        anchors.leftMargin: 20
        anchors.rightMargin: 20

        // app categories
        Rectangle {
            id: categoriesRect
            height: squareButtonWidth
            width: squareButtonWidth
            radius: 8
            anchors.horizontalCenter: parent.horizontalCenter
            color: Qt.rgba(255, 255, 255, 0)

            PlasmaCore.IconItem {
                id: categories
                source: "nfs-launcher-sort"
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.verticalCenter: parent.verticalCenter

                width: buttonImageWidth
                height: buttonImageWidth
            }

            readonly property string tipContent: i18n("app categories")
            ToolTip {
                id: categoriesTip
                delay: Kirigami.Units.toolTipDelay
            }

            MouseArea {
                anchors.fill: parent
                onClicked: root.currentShowAppsPage = root.currentShowAppsPage == 0 ? 1 : 0

                hoverEnabled: true
                onEntered: {
                    categoriesRect.color = Qt.rgba(255, 255, 255, 0.5)
                    categoriesTip.show(categoriesRect.tipContent, 2000)
                }

                onExited: {
                    categoriesRect.color = Qt.rgba(255, 255, 255, 0)
                    categoriesTip.hide()
                }
            }
        }

        Item {
            Layout.fillHeight: true
        }

        // user
        Rectangle {
            id: userRect
            height: squareButtonWidth
            width: squareButtonWidth
            radius: 8
            anchors.horizontalCenter: parent.horizontalCenter
            color: Qt.rgba(255, 255, 255, 0)

            Rectangle {
                id: userMark
                width: buttonImageWidth + 6 * PlasmaCore.Units.devicePixelRatio
                height: buttonImageWidth + 6 * PlasmaCore.Units.devicePixelRatio
                radius: buttonImageWidth / 2 + 2 * PlasmaCore.Units.devicePixelRatio

                anchors.horizontalCenter: parent.horizontalCenter
                anchors.verticalCenter: parent.verticalCenter
                clip: true
                visible: false
            }
            Image {
                id: userImage
                source: kuser.faceIconUrl
                anchors.centerIn: parent
                width: buttonImageWidth + 6 * PlasmaCore.Units.devicePixelRatio
                height: buttonImageWidth + 6 * PlasmaCore.Units.devicePixelRatio
                smooth:true
                fillMode: Image.PreserveAspectFit
                visible: false
            }
            OpacityMask {
                anchors.fill: userImage
                source: userImage
                maskSource: userMark
            }
            

            readonly property string tipContent: kuser.loginName
            ToolTip {
                id: userTip
                delay: Kirigami.Units.toolTipDelay
            }

            MouseArea {
                anchors.fill: parent
                onClicked: KQuickAddons.KCMShell.openSystemSettings("kcm_users")

                hoverEnabled: true
                onEntered: {
                    userRect.color = Qt.rgba(255, 255, 255, 0.5)
                    userTip.show(userRect.tipContent, 2000)
                }

                onExited: {
                    userRect.color = Qt.rgba(255, 255, 255, 0)
                    userTip.hide()
                }
            }
        }

        // computer
        Rectangle {
            id: computerRect
            height: squareButtonWidth
            width: squareButtonWidth
            radius: 8
            anchors.horizontalCenter: parent.horizontalCenter
            color: Qt.rgba(255, 255, 255, 0)

            PlasmaCore.IconItem {
                id: computer
                source: "nfs-launcher-computer"
                width: buttonImageWidth
                height: buttonImageWidth

                anchors.horizontalCenter: parent.horizontalCenter
                anchors.verticalCenter: parent.verticalCenter
            }

            readonly property string tipContent: i18n("computer")
            ToolTip {
                id: computerTipcurrentShowAppsPage
                delay: Kirigami.Units.toolTipDelay
            }

            MouseArea {
                anchors.fill: parent
                onClicked: action_openMyCompute()

                hoverEnabled: true
                onEntered: {
                    computerRect.color = Qt.rgba(255, 255, 255, 0.5)
                    computerTipcurrentShowAppsPage.show(computerRect.tipContent, 2000)
                }

                onExited: {
                    computerRect.color = Qt.rgba(255, 255, 255, 0)
                    computerTipcurrentShowAppsPage.hide()
                }
            }
        }

        // systemsettings
        Rectangle {
            id: systemsettingsRect
            height: squareButtonWidth
            width: squareButtonWidth
            radius: 8
            anchors.horizontalCenter: parent.horizontalCenter
            color: Qt.rgba(255, 255, 255, 0)

            PlasmaCore.IconItem {
                id: systemsettings
                source: "nfs-launcher-settings"
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.verticalCenter: parent.verticalCenter

                width: buttonImageWidth
                height: buttonImageWidth

            }

            readonly property string tipContent: i18n("systemsettings")
            ToolTip {
                id: systemsettingsTip
                delay: Kirigami.Units.toolTipDelay
            }

            MouseArea {
                anchors.fill: parent
                onClicked: KQuickAddons.KCMShell.openSystemSettings("kcm_landingpage")

                hoverEnabled: true
                onEntered: {
                    systemsettingsRect.color = Qt.rgba(255, 255, 255, 0.5)
                    systemsettingsTip.show(systemsettingsRect.tipContent, 2000)
                }

                onExited: {
                    systemsettingsRect.color = Qt.rgba(255, 255, 255, 0)
                    systemsettingsTip.hide()
                }
            }
        }

        // power
        Rectangle {
            id: powerRect
            height: squareButtonWidth
            width: squareButtonWidth
            radius: 8
            anchors.horizontalCenter: parent.horizontalCenter
            color: Qt.rgba(255, 255, 255, 0)

            PlasmaCore.IconItem {
                id: power
                source: "nfs-launcher-power"
                width: buttonImageWidth
                height: buttonImageWidth

                anchors.horizontalCenter: parent.horizontalCenter
                anchors.verticalCenter: parent.verticalCenter
            }

            readonly property string tipContent: i18n("power")
            ToolTip {
                id: powerTip
                delay: Kirigami.Units.toolTipDelay
            }

            MouseArea {
                anchors.fill: parent
                onClicked: action_powerButton()

                hoverEnabled: true
                onEntered: {
                    powerRect.color = Qt.rgba(255, 255, 255, 0.5)
                    powerTip.show(powerRect.tipContent, 2000)
                }

                onExited: {
                    powerRect.color = Qt.rgba(255, 255, 255, 0)
                    powerTip.hide()
                }
            }
        }
    }
}
